package com.toolset.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.toolset.Bean.User;
import com.toolset.R;

import org.litepal.crud.DataSupport;

import java.util.List;

public class LoginActivity extends BaseActivity {

    EditText userId;  // 账号框
    EditText userPassword;  //密码框
    CheckBox rememberPassword;  //记住密码框
    TextView registerBtn; //注册账号；
    Button loginBtn; //登录按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //初始化事件
        initEvent();

        //启动获取保存的账号密码
        getUserData();

        //设置注册账号的TextView可点击
        registerBtn.setClickable(true);

        //设置点击注册  跳转注册界面
        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });


        //登录操作
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = userId.getText().toString();
                String password = userPassword.getText().toString();

                //查询数据库
               List<User> userList =  DataSupport.where("userId = ? and userPassword = ?",id,password).find(User.class);
                if (userList.size()!=0){
                    //保存账号密码
                    rememberPassword(id,password);

                    User user = userList.get(0);
                    Toast.makeText(LoginActivity.this,"登陆成功！",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    //对象序列化传送 Bean实现Serializable接口
                    //接收需要 intent.getSerializableExtra("user");
                    intent.putExtra("user", user);
                    startActivity(intent);
                }else{ //数据库查询不到该数据
                    Toast.makeText(LoginActivity.this,"账号或者密码有误！或者不存在~",Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    //初始化事件
    private void initEvent(){
        userId = (EditText) findViewById(R.id.loginUserId);
        userPassword = (EditText) findViewById(R.id.loginUserPassword);

        rememberPassword = (CheckBox) findViewById(R.id.rememberUserPassword);

        registerBtn = (TextView) findViewById(R.id.registerIntentBtn);

        loginBtn = (Button) findViewById(R.id.loginBtn);
    }

    //记住密码 账号自动记住  密码打勾后就会记住
    private void rememberPassword(String userId,String userPassword){
        //实例化 SharedPreferences.Editor 对象 存储数据
        SharedPreferences.Editor editor = getSharedPreferences("UserData",MODE_PRIVATE).edit();
        //记住账号
        editor.putString("userId", userId);
        //记住密码
        if (rememberPassword.isChecked()){
            editor.putString("userPassword", userPassword);
        }
        editor.apply();
    }

    //启动获取保存的账号密码
    private void getUserData(){
        //实例化 SharedPreferences 对象 存储数据
        SharedPreferences pref = getSharedPreferences("UserData",MODE_PRIVATE);
        //获取账号
        userId.setText(pref.getString("userId",""));
        userId.setSelection(userId.getText().length());
        //获取密码
        userPassword.setText(pref.getString("userPassword",""));

    }

}
